package com.jl.basicframework.ui;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.alibaba.android.arouter.launcher.ARouter;
import com.jl.basicframework.listenerInterface.OnUiOperateListener;
import com.jl.basicframework.manager.ActivityManager;
import com.jl.basicframework.viewModel.BaseViewModel;
import com.xxr.lib_popup.ui.Dialogue;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import me.leefeng.promptlibrary.PromptDialog;

/***
 * @author 作者:姜澜
 * 时间:2022/3/8
 * 邮箱:316879104@pp.com
 * 类简述:Activity父类原则上所以Activity都需要继承他
 */
public abstract class BaseActivity
        <D extends ViewDataBinding,VM extends BaseViewModel>
        extends ParallaxBackActivity implements OnUiOperateListener {

    /**
     * dataBinding
     */
    protected D bind;

    /**
     * viewModel
     */
    protected VM viewModel;

    /**
     * Intent携带参数
     */
    protected String dataJson;

    /**
     * 加载弹出框
     */
    protected PromptDialog loadDialog;

    /**
     * 提示弹出框
     */
    protected Dialogue promptDialogue;

    /**
     * onCreate回调时 初始化数据
     */
    protected abstract void initData();

    /**
     * onResume回调时 重置试图
     */
    protected abstract void resetView();

    /**
     * 返回LayoutID
     * @return 视图页面id
     */
    protected abstract int getLayout();

    /**
     * 返回viewModel对象
     * @return vm
     */
    protected abstract VM getViewModel();

    /**
     * 设置状态栏颜色
     */
    public void setStatusBarColor(int color){
        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(color);
    }

    /**
     * 设置状态栏图标颜色
     */
    public void setStatusBarIconColorBlack(){
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }

    /**
     * 设置Toast
     */
    protected void showToast(String toastMessage){
        if (toastMessage==null || "".equals(toastMessage)){
            return;
        }
        Toast.makeText(this,toastMessage,Toast.LENGTH_LONG).show();

    }

    /**
     * 初始化dataBinding
     */
    protected void initDataBinding(){
        bind = DataBindingUtil.setContentView(this, getLayout());
        bind.setLifecycleOwner(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //将放入管理堆栈
        ActivityManager.getActivityManager().attach(this);

        //阿里路由自动注入初始化
        ARouter.getInstance().inject(this);

        //初始化消息总线
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }

        //初始化viewModel
        viewModel = getViewModel();
        //设置数据监听
        viewModel.setChangeListener(this);

        //获取其他activity发送的参数
        dataJson = getIntent().getStringExtra("activityDataJson");

        //初始化提示弹出框
        promptDialogue = new Dialogue(this);

        //初始加载弹出框
        loadDialog = new PromptDialog(this);

        //初始化dataBinding
        initDataBinding();
        //初始化数据
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消数据监听
        viewModel.clearDataChangeListener();

        //注销消息总线绑定
        EventBus.getDefault().unregister(this);

        //将Activity移出管理堆栈
        ActivityManager.getActivityManager().detach(this);



    }

    @Override
    protected void onResume() {
        super.onResume();

        //重置视图
        resetView();
    }

    /**
     * 必须要有一个Subscribe，测试时报错
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String event) {

    }

    /**
     * 关闭加载弹出框
     */
    @Override
    public void onLoadDismiss() {
        loadDialog.dismiss();
    }

    /**
     * 启动弹出框
     */
    @Override
    public void onLoadShow(String message) {
        loadDialog.showLoading(message);
    }

    /**
     * 通知更新列表
     */
    @Override
    public void onNotifyRefreshList() {

    }

    /**
     * 启动提示弹出框
     */
    @Override
    public void onPromptShow(String message) {
        promptDialogue.setTitle("温馨提示")
                .setMessage(message)
                .setPositiveButton("确定",null)
                .show();
    }

    /**
     * 跳转指定页面
     * @param activityPath 页面路径
     * @param data 携带的参数
     */
    @Override
    public void onToActivity(String activityPath, String data) {
        ARouter.getInstance()
                .build(activityPath)
                .withString("activityDataJson",data)
                .navigation();
    }

    /**
     * 启动Toast
     */
    @Override
    public void onToastShow(String message) {
        showToast(message);
    }
}
